interface Format {
    "M+": number,
    "d+": number,
    "h+": number,
    "m+": number,
    "s+": number,
    "q+": number,
    "S": number,
}

export function timeFormat(date: string | Date, fmt: string = 'yyyy-MM-dd hh:mm:ss') {

    if (typeof date === 'string') {
        date = new Date(date);
    }

    let o: Format = {
        "M+": date.getMonth() + 1,                 //月份   
        "d+": date.getDate(),                    //日   
        "h+": date.getHours(),                   //小时   
        "m+": date.getMinutes(),                 //分   
        "s+": date.getSeconds(),                 //秒   
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
        "S": date.getMilliseconds()             //毫秒   
    };

    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));

    for (let k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            const value = o[k as keyof Format];
            const exp = String((RegExp.$1.length == 1) ? value : (("00" + value).substr(("" + value).length)));
            fmt = fmt.replace(RegExp.$1, exp);
        }
    }

    return fmt;
}